<!DOCTYPE html>
<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>
<script>
function testCanvas(ctx, r, g, b, a) {
    var colorData = ctx.getImageData(50, 50, 1, 1).data;
    assert_equals(colorData[0], r);
    assert_equals(colorData[1], g);
    assert_equals(colorData[2], b);
    assert_equals(colorData[3], a);
}

function testTransferFromImageBitmapNullability(image, image2) {
    var bitmapCanvas = document.createElement('canvas');
    bitmapCanvas.width = bitmapCanvas.height = 100;
    var bitmapCtx = bitmapCanvas.getContext('bitmaprenderer');
    bitmapCtx.transferFromImageBitmap(image);

    // Make sure the bitmap renderer canvas is filled correctly.
    var myCanvas = document.createElement('canvas');
    myCanvas.width = myCanvas.height = 100;
    var myCtx = myCanvas.getContext('2d');
    myCtx.drawImage(bitmapCanvas, 0, 0);
    testCanvas(myCtx, 0, 255, 0, 255);

    // Test if passing null resets the bitmap renderer canvas.
    // Drawing the resetted canvas cannot change the destination canvas.
    bitmapCtx.transferFromImageBitmap(null);
    var myCanvas2 = document.createElement('canvas');
    myCanvas2.width = myCanvas2.height = 100;
    var myCtx2 = myCanvas2.getContext('2d');
    myCtx2.drawImage(bitmapCanvas, 0, 0);
    testCanvas(myCtx2, 0, 0, 0, 0);

    // Test if we can redraw the bitmap canvas correctly after reset.
    bitmapCtx.transferFromImageBitmap(image2);
    var myCanvas3 = document.createElement('canvas');
    myCanvas3.width = myCanvas3.height = 100;
    var myCtx3 = myCanvas3.getContext('2d');
    myCtx3.drawImage(bitmapCanvas, 0, 0);
    testCanvas(myCtx3, 255, 0, 0, 255);
}

function fillCanvas(canvas, style, r, g, b)
{
    var ctx = canvas.getContext('2d');
    ctx.fillStyle = style;
    ctx.fillRect(0, 0, 100, 100);
    testCanvas(ctx, r, g, b, 255);
}

promise_test(function() {
    var canvas = document.createElement('canvas');
    canvas.width = canvas.height = 100;
    fillCanvas(canvas, '#0f0', 0, 255, 0);

    var canvas2 = document.createElement('canvas');
    canvas2.width = canvas2.height = 100;
    fillCanvas(canvas2, '#f00', 255, 0, 0);

    return Promise.all([
        createImageBitmap(canvas),
        createImageBitmap(canvas2),
    ]).then(([image, image2]) => {
        testTransferFromImageBitmapNullability(image, image2);
    });
}, 'testTransferFromImageBitmapNullability should not return any error');

</script>

